package com.pansoft.openplanet.fragment;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.efounder.chat.db.WeChatDBManager;
import com.efounder.chat.utils.UshareHelper;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.mobilecomps.contacts.User;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.activity.LuckMoneyPosterActivity;
import com.utilcode.util.ReflectUtils;
import com.utilcode.util.ScreenUtils;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;

/**
 * 红包已备好dialog
 *
 * @author YQS 2018/08/15
 */
public class RedPackagePrepareDialogFragment extends DialogFragment implements View.OnClickListener {

    public static final int TYPE_SINGLE = 1;//
    public static final int TYPE_GROUP = 2;//群组红包
    public static final int TYPE_WEB = 3;//web 红包


    private static final String TAG = "RedPackagePrepareDialogFragment";
    private View rootView;
    private RelativeLayout topLayout;
    private LinearLayout llMessageLayout;
    private TextView tvNickname;
    private TextView tvStats;
    private Button btnSend;
    private Button btnPoster;

    private ImageView ivClose;

    private String shareUrl;
    private String totalMoney;
    private String unit;
    private String remark;
    private int redCount;

    private int type;

    /**
     * 红包已备好
     *
     * @param shareUrl   分享的url
     * @param redCount   红包个数
     * @param totalMoney 总金额
     * @param unit       代币单位
     * @return
     */
    public static RedPackagePrepareDialogFragment newInstance(String remark, String shareUrl, int redCount, String totalMoney, String unit, int type) {

        Bundle args = new Bundle();
        args.putString("shareUrl", shareUrl);
        args.putInt("redCount", redCount);
        args.putInt("type", type);
        args.putString("remark", remark);
        args.putString("totalMoney", totalMoney);
        args.putString("unit", unit);
        RedPackagePrepareDialogFragment fragment = new RedPackagePrepareDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //隐藏标题栏
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        rootView = inflater.inflate(R.layout.assets_dialog_red_package_prepared, container, false);
        initView(rootView);
        initData();
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setBackgroundDrawableResource(R.color.transparent);
        getDialog().getWindow()
                .setLayout(ScreenUtils.getScreenWidth() * 4 / 5, WindowManager.LayoutParams.WRAP_CONTENT);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().setCancelable(true);

    }

    private void initView(View rootView) {
        topLayout = (RelativeLayout) rootView.findViewById(R.id.top_layout);
        llMessageLayout = (LinearLayout) rootView.findViewById(R.id.ll_message_layout);
        tvNickname = (TextView) rootView.findViewById(R.id.tv_nickname);
        tvStats = (TextView) rootView.findViewById(R.id.tv_stats);
        btnSend = (Button) rootView.findViewById(R.id.btn_send);
        btnSend.setOnClickListener(this);
        btnPoster = (Button) rootView.findViewById(R.id.btn_poster);
        btnSend.setOnClickListener(this);
        btnPoster.setOnClickListener(this);
        ivClose = (ImageView) rootView.findViewById(R.id.iv_close);
        ivClose.setOnClickListener(this);
    }

    private void initData() {

        shareUrl = getArguments().getString("shareUrl");
        totalMoney = getArguments().getString("totalMoney");
        unit = getArguments().getString("unit");
        redCount = getArguments().getInt("redCount");
        type = getArguments().getInt("type");
        remark = getArguments().getString("remark");

        tvStats.setText(String.format(getResources().getString(R.string.luck_loney_prepared_text), redCount + "", totalMoney, unit));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_close) {
            dismiss();
        } else if (v.getId() == R.id.btn_send) {
            dismiss();
            if (type == TYPE_WEB) {
                User user = WeChatDBManager.getInstance().getOneUserById(Integer.valueOf(EnvironmentVariable.getProperty(CHAT_USER_ID)));
                StringBuffer stringBuffer = new StringBuffer(ResStringUtil.getString(R.string.wechatview_come_from));
                stringBuffer.append(user.getNickName())
                        .append(ResStringUtil.getString(R.string.wechatview_of))
                        .append(unit)
                        .append(ResStringUtil.getString(R.string.wechatview_red));
                ReflectUtils reflectUtils = ReflectUtils.reflect(EnvironmentVariable.getProperty("shareClass", ""));
                reflectUtils.newInstance().method("share", getActivity(), shareUrl,
                        remark, R.drawable.wechat_icon_launcher, stringBuffer.toString(),
                        new UshareHelper.ShareListener() {
                            @Override
                            public void share(int type) {
                                if (type == 1) {
                                    //分享成功
                                    //dismiss();
                                }
                            }
                        });
            }
        } else if (v.getId() == R.id.btn_poster) {
            //红包海报界面
            LuckMoneyPosterActivity.start(getActivity(), shareUrl, unit, totalMoney);
        }
    }

}
